﻿using CurrentUserDemo.Domain.CurrentUsers;
using CurrentUserDemo.Domain.OrderServices;
using DotNetCore.CAP;

namespace CurrentUserDemo.Api.EventHandlers;

public class OrderCreatedEventHandler : ICapSubscribe
{
    private readonly ICurrentUser _currentUser;
    private readonly OrderService _orderService;

    public OrderCreatedEventHandler(ICurrentUser currentUser, OrderService orderService)
    {
        _currentUser = currentUser;
        _orderService = orderService;
    }

    [CapSubscribe("test.show.time")]
    public void ReceiveMessage(DateTime time, [FromCap] CapHeader header)
    {
        Console.WriteLine("message time is:" + time);
        Console.WriteLine("message firset header :" + header["my.header.first"]);
        Console.WriteLine("message second header :" + header["my.header.second"]);
        Console.WriteLine("message uid header :" + header["my.header.uid"]);
        Console.WriteLine($"Current user id is: {_currentUser.GetUserId()}");
        Console.WriteLine($"Current order title: {_orderService.Create(name: header["my.header.orderName"]!)}");
    }
}
